function Thread (){
	var self = this;
	var Blob = (window.Blob|| window.WebKitBlob);
  	var URL = (window.URL || window.webkitURL);
  	var Worker = window.Worker;
	this.worker = null;
	if (URL && Blob && Worker) {	
		var b = new Blob([
			'___scope = {};'+
			'___scope._listeners=[];'+
			'___scope.fire = function(_type, _data){'+
				'var l = this._listeners.length;'+
				'while(l--){'+
					'var listener = this._listeners[l];'+
					'if(listener.type === _type){'+
						'listener.handler(_data, _type, listener);'+
					'};'+
				'};'+
			'};'+
			'___scope.on = function(_type, _handler){'+
				'this._listeners.push({type:_type, handler:_handler});'+
			'};'+
			
			
			'function ___uuidRecieve(id, doOnThread){	___scope.on(id,function(data, type, handler){eval(doOnThread);})};'+
			'function ___uuidFire(id, doOnMainThread){self.postMessage({cmd:"uuidGet",msg:doOnMainThread});};'+
			
		
			
			'___scope.on("exec", function(data, type, handler){'+
				'self.eval(data);'+
			'});'+
						
			'self.addEventListener("message", function(e){'+
				'___scope.fire(e.data.cmd, e.data.msg);'+
			'});'				
		],{"type":"application/javascript"});
		
		
		//'importScripts("workerscript.js");'+	
		//'scope.fire("newMessage__", __message);'+
			
			
		this.worker = new Worker(URL.createObjectURL(b));
		var ww = this.worker;
		this.worker.addEventListener('message',	function(e){
			var data = e.data;
			var cmd = data.cmd;
			var msg = data.msg;
			switch(cmd){
				case ('uuidGet'):
					eval(msg);
					console.log(cmd,msg, ww);
					ww.postMessage({cmd:cmd, msg: msg});
				break;
				
				case ('fire'):
					self.fire(e.data.type,e.data.msg);
				break;
				case ('set'):
					console.log(e,e.data);
				break;
				case ('log'):
					console.log(e,e.data.msg);
				break;
				
				default:
					console.log(e,e.data.cmd);
				break;
				
			}
		});				
	}else{
		console.error("could not create Worker Thread", URL, BlobBuilder, Worker);	
	}
	
	// extending thread with functionality
	this.exec('function emit(a){self.postMessage(a); };');
	this.exec('function log(){emit(self);}');
	this.exec('___scope.on( "get",function(data, type, handler){self.emit({cmd:"set", which:data });});');
	
};



Thread.prototype.exec = function(fn){
	var self = this;
	function parseFN (fn, type){
		var result = "";
		var str = fn.toString();
		
		var wins = str.split(type+".");
		
		var nFn = wins[0];
		
		if(wins.length >=2){
			var fx = wins[1];
			var _uuid = (Math.random()+""+Date.now()).toString().substr(2);
			nFn +='___uuidRecieve('+_uuid+', '+parseFN( wins.slice(2).join(type+'.') ,type)+'); ';
			nFn +='___uuidFire('+_uuid+', ' + '"'+type+'.'+fx+'"'   +');';
		}else{
			return str || null;
		}
		result = nFn;
		return result;
	};
	
	fn_Parsed = parseFN(parseFN(fn,"console"),"window");
	
	this.worker.postMessage({cmd:'exec', msg:fn_Parsed})
};
Thread.prototype.send = function(msg){
	this.worker.postMessage({cmd:'send', message:msg});
};
Thread.prototype.set = function(key, value){
	this.exec('var '+key+"="+value+";");
}

// TODO Implement get
Thread.prototype.get = function(key, handler){
	this.exec('fire('+"get"+',)');
}

